﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using ZR.Admin.WebApi.Extensions;
using ZR.Model.mes.md;
using ZR.Model.mes.md.DTO;
using ZR.Service.mes.md.IService;

namespace ZR.Admin.WebApi.Controllers.mes.md
{

    [Route("mes/md/unit")]
    public class MdUnitController : BaseController
    {
        private readonly IMdUnitService unitService;

        public MdUnitController(IMdUnitService unitService)
        {
            this.unitService = unitService;
        }
        /// <summary>
        /// 分页查寻 unti
        /// </summary>
        /// <param name="pageNum">页码</param>
        /// <param name="pagesize">页尺</param>
        /// <param name="name">单位名称</param>
        /// <param name="code">单位代码</param>
        /// <returns></returns>
        [HttpGet("list")]
        public IActionResult Getlist(int pageNum, int pagesize, string name = "", string code = "")
        {
            var unitPageDto = unitService.GetList(name, code, pageNum, pagesize);
            return SUCCESS(unitPageDto);
        }

        /// <summary>
        /// by pk  寻求unit
        /// </summary>
        /// <param name="measure_id"></param>
        /// <returns></returns>
        [HttpGet("getUnit/{measure_id}")]
        public IActionResult GetUnit(int measure_id)
        {
            var unit = unitService.GetUnitbyPK(measure_id);
            return SUCCESS(unit);
        }

        /// <summary>
        /// 添加Unit
        /// </summary>
        /// <param name="paramss"></param>
        /// <returns></returns>
        [HttpPost("addUnit")]
        public IActionResult AddUnitmeasure([FromBody] MdUnit paramss)
        {
            if(paramss == null) {
                return ToResponse(new ApiResult((int)ResultCode.NO_DATA, "null"));
            }
            paramss.ToCreate(HttpContext);
            int result = unitService.InsertUnit(paramss);
            return SUCCESS(result);
        }
        /// <summary>
        /// 更新单位unit
        /// </summary>
        /// <param name="paramss"></param>
        /// <returns></returns>

        [HttpPost("updateUnit")]
        public IActionResult UpdateUnit([FromBody] MdUnit paramss)
        {
            if (paramss != null)
            {
                paramss.ToUpdate(HttpContext);
                int result = unitService.Updateunit(paramss);

                return SUCCESS(result);
            }
            else
            {
                return ToResponse(ApiResult.Error(110, "请求参数为空"));
            }


        }
        /// <summary>
        /// 根据主键删除名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("delUnit/{id}")]
        public IActionResult DelUnit(string id)
        {
            int[] ids = Tools.SpitIntArrary(id);
            int result = unitService.deleteunit(ids);
            return ToResponse(result);
        }

        /// <summary>
        /// 导出Unit excel
        /// </summary>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        [HttpGet("exportUnit")]
        public IActionResult exportUnit([FromQuery] SearchOptionDTO searchOption)
        {
            List<MdUnit> units = null;
            if (searchOption != null)
            {
                units = unitService.GetList(searchOption);
            }

            var result = ExportExcelMini(units, "unit", "单位列表");
            return ExportExcel(result.Item2, result.Item1);

        }

  

    }

}

